﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace ATM_Group3.Data
{
    public class Account : Connection
    {
        private int _accountID;
        private float _balance;
        private String _nameCus;

        public int AccountID
        {
            get { return _accountID; }
            set { _accountID = value; }
        }       

        public float Balance
        {
            get { return _balance; }
            set { _balance = value; }
        }
        private Customer _customer;

        public Customer Customer
        {
            get { return _customer; }
            set { _customer = value; }
        }
        public void Withdraw(string AccountID, float amount)
        {
            SqlCommand cmd = new SqlCommand("sp_Withdraw", Connect());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@accountId", AccountID));
            cmd.Parameters.Add(new SqlParameter("@amount", amount));
            cmd.ExecuteNonQuery();
        }
  
        public bool CheckWithdrawCard(string AccountID, float amount)
        {
            float balance;
            string cmdStr = "select Balance from Account where AccountId = @accountId";
            SqlCommand cmd = new SqlCommand(cmdStr, Connect());
            cmd.Parameters.Add(new SqlParameter("@accountId", AccountID));
            cmd.ExecuteNonQuery();
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            SqlDataReader r = cmd.ExecuteReader();
            if (r.Read())
            {
                balance = float.Parse(r["Balance"].ToString());
                if ((balance - amount) > 0)
                    return true;
                else return false;
            }
            else return false;
        }

        public DataTable GetAcc(Data.Card obj)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand("Account_GetAcc", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@p1", obj.CARDID));
            da.SelectCommand = cmd;
            da.Fill(ds, "Account");
            DataTable dt = ds.Tables["Account"];
            return dt;
        }
    }
}